package com.stylefeng.guns.modular.flagglory.controller;

import com.fasterxml.uuid.impl.UUIDUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.util.UUID;

/**
 * Created by IntelliJ IDEA.
 * User: xinqi
 * Date: 2018/2/3
 * Time: 10:30
 */
@RestController
@RequestMapping("/upload")
public class uploadController {
    //上传路径
    private final static String UPLOAD_URL = "/static/upload";
    //图片路径
    private final static String IMAGE_Url = "/image";
    //头像地址
    private final static String HEAD_IMAGE_URL = "/headImage";
    //段位地址
    private final static String DIVISION_IMAGE_URL = "/divisionImage";

    @RequestMapping("/uploadHeadImage")
    public String uploadHeadImage(@RequestParam("file") MultipartFile image, HttpServletRequest request) throws IOException {
        return upload(image, IMAGE_Url + HEAD_IMAGE_URL, request);
    }

    @RequestMapping("/uploadDivisionImage")
    public String uploadDivisionImage(@RequestParam("file") MultipartFile image, HttpServletRequest request) throws IOException {
        return upload(image, IMAGE_Url + DIVISION_IMAGE_URL, request);
    }

    private String upload(MultipartFile uploadFile, String folderName, HttpServletRequest request) throws IOException {
        String addr = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/" ;
        String path = Thread.currentThread().getContextClassLoader().getResource("").getPath() + UPLOAD_URL + folderName;
        if (!uploadFile.isEmpty()) {
            File uploadFolder = new File(path);
            if (!uploadFolder.exists()) {
                uploadFolder.mkdirs();
            }
            //截取类型
            String originalFilename = uploadFile.getOriginalFilename();
            String[] split = originalFilename.split("\\.");
            String type = "." + split[1];
            UUID uuid = UUID.randomUUID();
            String imageName = uuid.toString() + type;
            String imagePath = uploadFolder + "/" + imageName;
            File file = new File(imagePath);
            while (file.exists()) {
                imageName = "1" + imageName;
                imagePath = uploadFolder + "/" + imageName;
                file = new File(imagePath);
            }
            try {
                uploadFile.transferTo(new File(imagePath));
            } catch (IOException e) {
                throw new IOException("图片上传失败！");
            }
            return addr + UPLOAD_URL + folderName + "/" + imageName;
        }
        throw new IOException("未上传图片！");
    }
}
